define([], function () {
	var app = angular.module("App.home");
	app.controller("projects.detail.typesCtrl", ["$scope", "Project", "curPrj", function ($scope, Project, curPrj) {
		$scope.allTypes = [];
		$scope.curTypes = [];
		$scope.stations = [];
		$scope.showAutoSurvey = false;
		Project
			.getAllMonitorTypes()
			.then(function (data) {
				$scope.allTypes = data;
				return curPrj.getMonitorTypes();
			}, function () {
				console.log("Error retrieving monitor types.");
			})
			.then(function (data) {
				$scope.curTypes = data;
			});

		$scope.toggle = function (type) {
			var index = $scope.curTypes.indexOf(type);
			if (index == -1) {
				$scope.curTypes.push(type);
			} else {
				$scope.curTypes.splice(index, 1);
			}
		};

		$scope.$watch("curTypes", function (newValue) {
			if (!newValue)
				return;
			if (newValue.indexOf("自动化收敛") > -1) {
				Project
					.getAllAutoSurveyStations(curPrj)
					.then(function (data) {
						$scope.stations = data;
					});
				$scope.showAutoSurvey = true;
			} else {
				$scope.showAutoSurvey = false;
			}
		}, true);

		$scope.bindStation = function (st) {
			if (!st.bind && st.prjid > -1)
				return;
			st.bind = !st.bind;
			if (st.bind) {
				st.prjid = curPrj.prjId;
			} else {
				st.prjid = -1;
			}
		};

		$scope.confirm = function () {
			$scope.status = "uploading";
			curPrj
				.postMonitorTypes($scope.curTypes)
				.then(function () {
					$scope.status = "succeeded";
				}, function () {
					$scope.status = "failed";
				});
		};

	}]);
});